from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.db import models
from django.conf import settings
from published_manager import PublishedManager, STATE_CHOICES, STATE_DEFAULT
from django.utils.translation import ugettext as _

class Poll(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique=True)
    description = models.TextField(blank=True, null=True)
    pub_date = models.DateTimeField(auto_now_add=True)
    state = models.CharField(max_length=1, choices=STATE_CHOICES, default=STATE_DEFAULT)
    ip_address = models.IPAddressField()
    user = models.ForeignKey(User)
    objects = PublishedManager()

    def get_absolute_url(self):
        return reverse("poll_view", kwargs={'poll_id': self.pk})

    def __unicode__(self):
        return _(self.title)

    class Meta:
        ordering = ['pub_date']
        get_latest_by = "pub_date"

class Choice(models.Model):
    poll = models.ForeignKey(Poll, null=False, blank=False)
    choice = models.CharField(max_length=200)

    def __unicode__(self):
        return _(self.choice)

class Vote(models.Model):
    poll = models.ForeignKey(Poll)
    choice = models.ForeignKey(Choice)
    pub_date = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, related_name='polls_vote')
    ip_address = models.IPAddressField()

    def __unicode__(self):
        return _(self.choice.choice)

    class Meta:
        unique_together = (("user", "poll"),)
        ordering = ['pub_date']
        get_latest_by = "pub_date"
